package com.uft.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.uft.model.dao.PreferencesDAO;
import com.uft.model.entity.ConfigProperty;
import com.uft.model.entity.Preferences;
import com.uft.model.entity.Profile;
import com.uft.model.entity.ProfilePreferences;


@Controller
public class PreferencesController {
	 @Autowired
	 private PreferencesDAO preferencesDAO;
	 private  static Logger  logger = Logger.getLogger(PreferencesController.class);
	 
	 @RequestMapping(value="/getPreferencesList", method = RequestMethod.POST)
	 @ResponseBody
	 public String getPreferencesList(HttpServletRequest request) {
		 String result="";
			List<Preferences> prefList=new ArrayList<Preferences>();
			try{
				prefList=preferencesDAO.getAllPreferences();
				ObjectMapper mapper = new ObjectMapper();
				String listData=mapper.writeValueAsString(prefList);
				//System.out.println("List dat: "+listData);
				result="{\"Result\":\"OK\",\"Records\":"+listData+"}";
			}
			catch(Exception ex){
				result="{\"Result\":\"ERROR\",\"Message\":\""+ex.getMessage()+"\"}";
				ex.printStackTrace();
			}
			
			return result;
	 }
	 
	@RequestMapping(value="/addPreference", method = RequestMethod.POST)
	@ResponseBody
	public String addPreference(Preferences pref,HttpServletRequest request){
		String result="";
		try{
		//	Preferences pref= getBeanFromRequest(request);
			System.out.println("Pref Name: "+pref.getPrefName());
			preferencesDAO.addPreferences(pref);
			ObjectMapper mapper = new ObjectMapper();
			String listData=mapper.writeValueAsString(pref);
			result="{\"Result\":\"OK\",\"Record\":"+listData+"}";
		}catch(Exception ex){
			result="{\"Result\":\"ERROR\",\"Message\":\""+ex.getMessage()+"\"}";
			ex.printStackTrace();
			
		}
		
		return result;
	}
	@RequestMapping(value="/deletePreference", method = RequestMethod.POST)
	@ResponseBody
	public String deletePreference(@RequestParam("preferenceId") int preferenceId,HttpServletRequest request){
		String result="";
		try{
			
				   System.out.println("preference id to delete: "+preferenceId);
				   preferencesDAO.deletePreferencesById(preferenceId);
				  result="{\"Result\":\"OK\"}";
			
		}catch(Exception ex){
			result="{\"Result\":\"ERROR\",\"Message\":\""+ex.getMessage()+"\"}";
			ex.printStackTrace();
			
		}
		return result;
		
	}
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	@RequestMapping(value="/updatePreference", method = RequestMethod.POST)
	@ResponseBody
	public String updatePreference( Preferences pref ,HttpServletRequest request){
		String result="";
		try{
			
			logger.debug("getPreferenceId: "+pref.getPreferenceId());
			preferencesDAO.updatePreferences(pref);
			result="{\"Result\":\"OK\"}";
		}catch(Exception ex){
			result="{\"Result\":\"ERROR\",\"Message\":\""+ex.getMessage()+"\"}";
			ex.printStackTrace();
			
		}
		logger.debug(result);
		return result;
		
	}
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	@RequestMapping(value="/getPreferencesView", method = RequestMethod.GET)
	public ModelAndView getPreferencesView(HttpServletRequest request,  
            HttpServletResponse response){
		ModelAndView model = new ModelAndView("preferences");
//		HashMap<String, List<Preferences>> prefList= new HashMap<String,List<Preferences>>();
//		List<String> catNames=preferencesDAO.getDistinctCategoryName();
//		for(String c:catNames){
//			List<Preferences> prefs=preferencesDAO.getPreferencesByCategory(c);
//			prefList.put(c, prefs);
//		}
		//logger.debug("prefList: "+prefList.size());
		model.addObject("preferencesDAO", preferencesDAO);	
		logger.debug("Get Preferences Lists");
		return model;
	}
	
//	public Preferences getBeanFromRequest(HttpServletRequest request){
//		Preferences pref= new Preferences();
//		if(request.getParameter("preferenceId")!=null){	
//			   int prefId=Integer.parseInt(request.getParameter("preferenceId"));
//			   pref.setPreferenceId(prefId);
//		 }
//		if(request.getParameter("prefName")!=null){	
//			   String prefName=request.getParameter("prefName");
//			   pref.setPrefName(prefName);;
//		 }
//		if(request.getParameter("prefValue")!=null){	
//			   String prefValue=request.getParameter("prefValue");
//			   pref.setPrefValue(prefValue);;
//		 }
//		if(request.getParameter("prefCategory")!=null){	
//			   String prefCategory=request.getParameter("prefCategory");
//			   pref.setPrefCategory(prefCategory);;
//		 }
//		return pref;
//	}
}
